<?php
/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-18
 * Time: 下午11:18
 */

class Pic extends MY_Action_Controller{



    function __construct(){
        parent::__construct();
        $this->auth();
    }

    /**
     * 提交图片
     *
     * @return json
     */
    function do_upload(){
        $image_type = $_REQUEST['type'];
        switch($image_type){
            case 'do_auth':
                //todo 图片服务器  不能被URL直接访问
                $this->_upload($image_type);
                break;
            case 'avatar':
            case 'trip':
            case 'trip_cover':
                //顽皮兔
                $result = $this->_upload_wanpitu($image_type);
                break;
            default:
                $this->set_response(50800);
        }
        $this->set_response(0,$result);
    }

    private function _upload($image_type,$field='Filedata'){
        $config_setting_name = $image_type;
        $upload = $this->upload_image($field, $config_setting_name);
        if (!$upload) {
            $this->set_response(50800);
        }
    }

    private function _upload_wanpitu($image_type,$field='Filedata'){
        $this->load->config('upload_images');
        $this->load->helper('string');
        $settings = $this->config->item($image_type);
        $config = array();
        $config['allowed_types'] = $settings['allowed_types'];
        $config['max_size'] = $settings['max_size'];
        $config['file_name'] = random_string('md5').random_string();
        $this->load->library('Alibaba_image');
        $this->alibaba_image->init($config);
        if(!$this->alibaba_image->check_file($field)){
            $this->set_response(50800,$this->alibaba_image->error_msg);
        }else{
            $this->load->library('Alibaba_image');
            $result = $this->alibaba_image->do_upload($this->_login_user['user_id'],
                $config['file_name'].$this->alibaba_image->file_ext,$this->alibaba_image->file_temp);
            if(!$result)
                $this->set_response(50800);
            return $result;
        }
    }

    function test(){
        $this->load->library('Alibaba_image');
        //var_dump($this->alibaba_image);
    }


}